package hc.um.action;

import hc.um.ScheduleDetail;
import hc.um.service.UserService;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CancelAppointmentServlet
 */
@WebServlet("/CancelAppointmentServlet")
public class CancelAppointmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private UserService userService = new UserService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CancelAppointmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		try 
		{
			this.modifyScheduleDetail(request, response);
		} 
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	public void modifyScheduleDetail(HttpServletRequest request,
			HttpServletResponse response) throws IOException, SQLException {
		ScheduleDetail scheduledetail = new ScheduleDetail();		
		
		scheduledetail.setScheduleID(Integer.parseInt(request.getParameter("scheduleid")));
		scheduledetail.setPatientID("");
		scheduledetail.setIsAvailable(1); 
		
		if ( userService.updateScheduleDetail(scheduledetail) == 1 )
		{
			response.getWriter().write(creatXmlTextModifyScheduleDetail("Success","Cancel the appointment successfully!"));
		} 
		else 
		{
			response.getWriter().write(creatXmlTextModifyScheduleDetail("Fail","Cancel the appointment failed."));
		}
	}
	
	public String creatXmlTextModifyScheduleDetail(String result, String message) {
		String xmltext = null;

		xmltext = "<cancelappointment><result>" + result + "</result><message>"
				+ message + "</message></cancelappointment>";
		System.out.println("xmltext="+xmltext);
		
		return xmltext;
	}	

}
